package mbean;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import util.MediaData;
import vo.ProcessVO;
import dao.ProcessDAO;

public class WfMBean {
	
	private Map<Integer, ProcessVO> mapaProcesso;	
	private final int wInicio = 23;
	private final int hInicio = 23;
	private final int wArrow = 106;
	private final int hArrow = 16;
	private List<String> listaCoordenada;
	private ProcessDAO dao;
	
	/*public String open(){
		int x,y;
		String coord;
		
		x = y = 0;
		
		dao = new ProcessDAO();
		mapaProcesso = dao.listProcess();
		
		listaCoordenada = new ArrayList<String>();
		
        x += this.wInicio + this.wArrow;
        	
        ProcessVO vo = dao.getInitialProcess();
        
        coord = Integer.toString(x) + "," + Integer.toString(y) + "," + Integer.toString(x + (((vo.getName().length()) * 6) + 2)) + "," + Integer.toString(y + 40);
        listaCoordenada.add(coord);
        
    	x += ((vo.getName().length()) * 6) + 2;
    	
    	int i = 1;
    	boolean direita = true;
    	
    	while(vo.getNextProcessId() > 0){
    		i++;
    		vo = mapaProcesso.get(vo.getNextProcessId());
    		
    		if (direita){
    	        x += this.wArrow;    			
    		}
    		
            coord = Integer.toString(x) + "," + Integer.toString(y) + "," + Integer.toString(x + (((vo.getName().length()) * 6) + 2)) + "," + Integer.toString(y + 40);
            listaCoordenada.add(coord);
        	
        	x += ((vo.getName().length()) * 6) + 2;
        	
        	if (i > 3){
        		i = 0;
        		direita = ! direita;
        	}
    	}
		
		return "wf";
	}
	
	public void paint(OutputStream out, Object data) throws IOException{
		BufferedImage bi;
		int x, y;
		
		x = y = 0;
		
		URL urlArquivo = getClass().getClassLoader().getResource("files");
		
		MediaData paintData = new MediaData();
		
        BufferedImage img = new BufferedImage(paintData.getWidth(),paintData.getHeight(),BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setBackground(paintData.getBackground());
        g2d.setColor(paintData.getDrawColor());
        g2d.clearRect(0,0,paintData.getWidth(),paintData.getHeight());

        bi = ImageIO.read(new File(urlArquivo.getPath() + "/startProcess.png"));
        g2d.drawImage(bi, null, x, y + 20 - (this.hInicio / 2));
        
        x += this.wInicio;
        	
        bi = ImageIO.read(new File(urlArquivo.getPath() + "/aRight.png"));
        g2d.drawImage(bi, null, x, y + 20 - (this.hArrow / 2));
        
        x += this.wArrow;
        
        ProcessVO vo = dao.getInitialProcess();

    	g2d.drawRect(x, y + 1, (vo.getName().length()) * 6, 40);
    	g2d.drawString(vo.getName(), x + 2, y + 20);
    	
    	x += ((vo.getName().length()) * 6) + 2;
    	
    	int i = 1;
    	boolean direita = true;
    	
    	while(vo.getNextProcessId() > 0){
    		i++;
    		vo = mapaProcesso.get(vo.getNextProcessId());
    		
    		if (direita){
    	        bi = ImageIO.read(new File(urlArquivo.getPath() + "/aRight.png"));
    	        g2d.drawImage(bi, null, x, y + 20 - (this.hArrow / 2));
    	        
    	        x += this.wArrow;    			
    		}
    		
        	g2d.drawRect(x, y + 1, (vo.getName().length()) * 6, 40);
        	g2d.drawString(vo.getName(), x + 2, y + 20);
        	
        	x += ((vo.getName().length()) * 6) + 2;
        	
        	if (i > 3){
        		i = 0;
        		direita = ! direita;
        	}
    	}

        ImageIO.write(img,"jpeg",out);        
    }
	
	public void paintInicio(OutputStream out, Object data) throws IOException{
		paint(out, data);
	}
	
	public void paintSetaDireita(OutputStream out, Object data) throws IOException{
		paint(out, data);
	}
	
	public void paintProcesso(OutputStream out, Object data) throws IOException{
		paint(out, data);
	}

	public void setListaCoordenada(List<String> listaCoordenada) {
		this.listaCoordenada = listaCoordenada;
	}

	public List<String> getListaCoordenada() {
		return listaCoordenada;
	}*/
}
